﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace EIA.WCFService.AuthProxy
{
    public class AuthenticationProxy
    {
        private AuthService.AuthenticationServiceClient GetClient()
        {
            AuthService.AuthenticationServiceClient client =
                string.IsNullOrEmpty(X.ServiceAddress) ? new AuthService.AuthenticationServiceClient() : new AuthService.AuthenticationServiceClient("BasicHttpBinding_IAuthenticationService", X.ServiceAddress);

            return client;
        }

        public void Login(string loginid, string pwd, Action<string> callback, Action<Exception> errorCallback = null)
        {
            AuthService.AuthenticationServiceClient client = GetClient();
            client.LoginCompleted += (s, e) => {
                if (e.Error != null)
                {
                    if (errorCallback != null) errorCallback(e.Error);
                }
                else
                {
                    if (callback != null) callback(e.Result);
                }
            };
            client.LoginAsync(loginid, pwd);
        }

        public void Logout(string token, Action<int> callback, Action<Exception> errorCallback = null)
        {
            AuthService.AuthenticationServiceClient client = GetClient();
            client.LogoutCompleted += (s, e) =>
            {
                if (e.Error != null)
                {
                    if (errorCallback != null) errorCallback(e.Error);
                }
                else
                {
                    if (callback != null) callback(e.Result);
                }
            };
            client.LogoutAsync(token);
        }

        public void GetUniqueId(string token, Action<string> callback, Action<Exception> errorCallback = null)
        {
            AuthService.AuthenticationServiceClient client = GetClient();
            client.GetUniqueIdCompleted += (s, e) =>
            {
                if (e.Error != null)
                {
                    if (errorCallback != null) errorCallback(e.Error);
                }
                else
                {
                    if (callback != null) callback(e.Result);
                }
            };
            client.GetUniqueIdAsync(token);
        }

        public void GetLoginTime(string token, Action<string> callback, Action<Exception> errorCallback = null)
        {
            AuthService.AuthenticationServiceClient client = GetClient();
            client.GetLoginTimeCompleted += (s, e) =>
            {
                if (e.Error != null)
                {
                    if (errorCallback != null) errorCallback(e.Error);
                }
                else
                {
                    if (callback != null) callback(e.Result);
                }
            };
            client.GetLoginTimeAsync(token);
        }

        public void ChangePassword(string loginid, string oldpwd, string newpwd, Action<bool> callback, Action<Exception> errorCallback = null)
        {
            AuthService.AuthenticationServiceClient client = GetClient();
            client.ChangePasswordCompleted += (s, e) =>
            {
                if (e.Error != null)
                {
                    if (errorCallback != null) errorCallback(e.Error);
                }
                else
                {
                    if (callback != null) callback(e.Result);
                }
            };
            client.ChangePasswordAsync(loginid, oldpwd, newpwd);
        }
    }
}
